//
//  AppointmentAntiTheftVC.m
//  Toyota
//
//  Created by ld on 2024/6/27.
//

#import "AppointmentAntiTheftVC.h"
#import "AppointmentAntiTheftCell.h"
#import "AddAppointmentVC.h"
#import "AppointmentAntiTheftModel.h"
#import "NoDataView.h"
#import "Definition.h"
@interface AppointmentAntiTheftVC ()<UITableViewDelegate, UITableViewDataSource> {
    NoDataView * noDataView;
}
@property (strong, nonatomic) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation AppointmentAntiTheftVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"预约防盗模式";
    self.view.backgroundColor = kColorFFFFFF;
    
    UIBarButtonItem * rightBar = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"used_car_add"] style:(UIBarButtonItemStylePlain) target:self action:@selector(addBarAction)];
    self.navigationItem.rightBarButtonItem = rightBar;
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = kColorFFFFFF;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 5)];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-CYSafeAreaBottom);
    }];
    
    [self reloadData];
    // Do any additional setup after loading the view.
}

-(void)reloadData {
    [noDataView removeFromSuperview];
    if (self.dataArr.count == 0) {
        noDataView = [[NoDataView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - Height_StatusBar - 44)];
        [noDataView hasImg:YES andTitleStr:@"暂无数据" andImgStr:@"noDataImg" andHeight:CYScalXH(170)];
        [self.tableView addSubview:noDataView];
    }
    [self.tableView reloadData];
}

-(void)addBarAction {
    AddAppointmentVC * vc = [[AddAppointmentVC alloc]init];
    vc.title = @"添加日期";
    vc.addModelBlock = ^(AppointmentAntiTheftModel * _Nonnull model) {
        [self.dataArr addObject:model];
        [self reloadData];
    };
    [self.navigationController pushViewController:vc animated:NO];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppointmentAntiTheftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppointmentAntiTheftCell"];
    if (!cell) {
        cell = [[AppointmentAntiTheftCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AppointmentAntiTheftCell"];
    }
    while ([cell.contentView.subviews lastObject] != nil)
    {
        [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
    }
    AppointmentAntiTheftModel * model = _dataArr[indexPath.row];
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cell.backgroundColor = kColorFFFFFF;
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    [cell setData:model index:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AddAppointmentVC * vc = [[AddAppointmentVC alloc]init];
    vc.title = @"修改日期";
    vc.editModel = _dataArr[indexPath.row];
    vc.addModelBlock = ^(AppointmentAntiTheftModel * _Nonnull model) {
        [self.dataArr replaceObjectAtIndex:indexPath.row withObject:model];
        [self reloadData];
    };
    [self.navigationController pushViewController:vc animated:NO];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
 
-(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 自定义删除按钮的样式
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault  title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self.dataArr removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    rowAction.backgroundColor = [UIColor colorWithHexString:@"EB0A1E"];
    NSArray *arr = @[rowAction];
    return arr;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
